<template>
  <div class="table-page-search-wrapper">
    <a-card :bordered="false">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="开课院系">
              <a-select :disabled="selectMenu.yxList.length === 1" allowClear v-model="queryParam.kkyxm" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="开课学年">
              <a-select allowClear v-model="queryParam.kkxn">
                <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="开课学期">
                <a-select allowClear v-model="queryParam.kkxqm">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="教师姓名">
                <a-input v-model.trim="queryParam.jsxm"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="原任课教室">
                <a-input v-model="queryParam.yrkjs"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="课程名称">
                <a-select allowClear v-model="queryParam.kcbm" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.courseList" :key="i.id" :value="i.kch">{{i.kch}}-{{ i.kcmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="8" :sm="12">
              <a-form-item label="调课状态">
                <a-select allowClear v-model="queryParam.shzt">
                  <a-select-option value="02">已提交</a-select-option>
                  <a-select-option value="03">院系审核已通过</a-select-option>
                  <a-select-option value="05">院长审核已通过</a-select-option>
                  <a-select-option value="07">培养办审核已通过</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
          </template>
        </a-row>
        <a-row>
          <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button type="primary" style="margin:0 10px" @click="exportExcel">导出</a-button>
          </span>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="{show: true}"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :scroll="{ x: 1500 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="kkyxm" slot-scope="kkyxm">
          {{ kkyxm | filterYx(selectMenu.yxList) }}
        </span>
        <span slot="shzt" slot-scope="shzt">
          {{ shzt | filterCode(TYPE0518) }}
        </span>

        <template slot="operation" slot-scope="a,b">
          <a @click="toDetail(b.id)">详情</a>
        </template>
      </s-table>
    </a-card>

    <checkModal
      ref="check"
      :visible="detailVisible"
      :isCheck="false"
      :yxList="selectMenu.yxList"
      @cancel="detailVisible = false"
      @refresh="$refs.table.refresh(false)"
    />

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import checkModal from '../components/checkModal'

export default {
  data () {
    return {
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          width:80,
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '所属院系',
          dataIndex: 'kkyxm',
          scopedSlots: { customRender: 'kkyxm' },
        },
        {
          title: '任课老师',
          width: 100,
          dataIndex: 'dsbhxm'
        },
        {
          title: '课程编号',
          width: 100,
          dataIndex: 'kcbm'
        },
        {
          title: '原任课教室',
          dataIndex: 'yrkjs'
        },
        {
          title: '调整后教室',
          dataIndex: 'tzhjs'
        },
        {
          title: '申请人',
          dataIndex: 'sqr'
        },
        {
          title: '审核时间',
          dataIndex: 'shsj'
        },
        {
          title: '审核状态',
          dataIndex: 'shzt',
          scopedSlots: { customRender: 'shzt' },
        },
        {
          title: '审核人',
          dataIndex: 'shr'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width:80,
          fixed: 'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        this.queryParam.shzt = '07'
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.tkList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {
        yxList: [], // 院系
      },
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      transferVisible: false,
      studentsVisible: false,
      initDone: false,
      detailVisible: false
    }
  },
  components: {STable, checkModal},
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0072: state => state.app['TYPE0072'],
      TYPE0518: state => state.app['TYPE0518'],
    })
  },
  filters: {
    filterCode(value, type) {
      let name = null
      type && type.map(item => {
        if(item.code === value) return name = item.name
      })
      return name
    },
    filterYx(value, yxList) {
      let name = null
      yxList.map(item => {
        if(item.dwh === value) return name = item.dwmc
      })
      return name
    }
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0518')
    this.initSelectMenu()
    if (this.$route.query.id) {
      this.toDetail(this.$route.query.id)
    }
  },
  methods: {
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        this.$set(this.queryParam, 'kkxqm', res[1].currentSemester)
        this.$set(this.queryParam, 'kkxn', res[1].schoolYear)
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'kkyxm', this.selectMenu.yxList[0].dwh)
        }
        this.initDone = true
      })
    },
    beforeUpload (file) {
      if (file.name.split('.')[1] !== 'xlsx') {
        this.$message.error('只支持.xlsx格式')
        return false
      }
    },
    handleChange (info) {
      if (info.file.status === 'done') {
        this.$message.success('导入成功');
        this.$refs.table.refresh(false)
      } else if (info.file.status === 'error') {
        this.$message.error(info.file.response.msg || '导入失败');
      }
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    toAdd () {
      this.$router.push('/cultivate/arrangeCourse/add')
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的排课信息')
        return
      }
      this.$confirm({
        title: '确定删除所选中的排课吗?',
        content: '当您点击确定按钮后，这些记录将会被删除',
        centered: true,
        onOk: () => {
          let ids = this.selectedRowKeys.join(',')
          this.$api.cultivate.pkxxDelete(ids).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    yxChange (code) {
      this.$set(this.queryParam, 'kcbm', '')
      if (code) {
        this.kcLoading = true
        this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
          this.$set(this.selectMenu, 'courseList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'courseList', [])
      }
    },
    showTransfer (c) {
      this.$refs.transfer.pkxxId = c.id
      this.$refs.transfer.note = c.note
      this.transferVisible = true
      // this.$api.cultivate.arrangeCourseDetail(c.id).then(res => {
      //   this.$refs.transfer.form.setFieldsValue(res.data)
      // })
    },
    toDetail (id) {
      this.detailVisible = true
      this.$api.cultivate.tkDetail(id).then(res => {
        this.$nextTick(() => {
          this.$refs.check.detail = res.data
        })
      })
    },
    showStudents (c) {
      this.studentsVisible = true
      this.$refs.students.pkxxId = c.id
      this.$nextTick(() => {
        this.$refs.students.$refs.table.refresh(true)
      })
    },
    exportExcel () {
      let params = this.queryParam
      delete params.pageNum
      delete params.pageSize
      this.$export('/foster/tksq/listExp', {
        ...params
      })
    }
  }
}
</script>
